#!/usr/bin/env bash

printf "\033c"
echo_cyan(){
  printf '\033[1;36m%b\033[0m\n' "$@"
}
echo_green(){
  printf '\033[1;32m%b\033[0m\n' "$@"
}
echo_red(){
  printf '\033[1;31m%b\033[0m\n' "$@"
}
echo_yellow(){
  printf '\033[1;33m%b\033[0m\n' "$@"
}

#系统更新到最新
upgradeOS(){
  echo_yellow "=================================================================="
  echo_red    "请使用纯净版操作系统，否则可能会造成系统破坏和数据丢失!!!"
  echo_green  "操作前请将系统更新至最新，指令如下："
  echo_yellow "=================================================================="
  apt update
  apt full-upgrade -y
  echo_red "条件允许，建议重启系统"
}

#清空原先数据
cleanOldData(){
  echo_yellow "=================================================================="
  echo_green "清理旧数据"
  echo_yellow "=================================================================="
  echo_cyan "清理systemctl单元"
  systemctl disable --now {postgres,php84-fpm,nginx}.service
  rm /lib/systemd/system/{postgres,php84-fpm,nginx}.service
  systemctl daemon-reload
  echo_cyan "清理旧目录 /server,/www 如果有重要数据请先备份"
  rm -rf /server /www
  echo_cyan "删除旧用户 postgres,php-fpm,nginx 如果有重要数据请先备份"
  userdel -r postgres
  userdel -r php-fpm
  userdel -r nginx
  groupdel postgres
  groupdel php-fpm
  groupdel nginx
}

#创建单个用户
createSingleUser(){
  userName=$1
  isSupportZsh=$2
  echo_green "创建 $userName 用户"
  groupadd $userName
  useradd -c "$userName service main process user" -g $userName -s /sbin/nologin -m $userName
  if [[ "$isSupportZsh" == '1' ]]; then
    cp -r /root/{.oh-my-zsh,.zshrc} /home/$userName
    chown $userName:$userName -R /home/$userName/{.oh-my-zsh,.zshrc}
  fi
}

#创建用户
createUser(){
  echo_yellow "=================================================================="
  echo_green "创建postgres,php-fpm,nginx的进程用户"
  echo_yellow "=================================================================="
  echo_red "必须root用户安装并配置成功zsh，才允许支持zsh"
  zshState=0
  echo_cyan "是否支持启用zsh(1支持，默认不支持)："
  read zshState
  createSingleUser 'postgres' $zshState
  createSingleUser 'php-fpm' $zshState
  createSingleUser 'nginx' $zshState
  echo ' '
  echo_yellow "=================================================================="
  echo_green "处理php-fpm的socket文件授权问题"
  echo_yellow "当 php-fpm 主进程非特权用户时，需要考虑socket文件权限问题："
  echo_yellow "nginx 如果是通过 sock 文件代理转发给 php-fpm，php-fpm 主进程创建\n sock 文件时需要确保 nginx 子进程用户有读写 sock 文件的权限"
  echo_yellow " "
  echo_yellow "方式1：采用 sock 文件权限 php-fpm:nginx 660 \n(nginx 权限较少，php-fpm 权限较多)"
  echo_cyan "usermod -a -G nginx php-fpm"
  echo_yellow " "
  echo_yellow "方式2：采用 sock 文件权限 php-fpm:php-fpm 660 \n(nginx 权限较多，php-fpm 权限较少)"
  echo_cyan "usermod -a -G php-fpm nginx"
  echo_yellow " "
  echo_green "此版本使用的是tcp转发，并不需要考虑socket文件转发相关的权限问题"
  echo_yellow "=================================================================="
  echo ' '
  echo_yellow "=================================================================="
  echo_green "php编译pgsql扩展，使用指定Postgres安装目录时，需要提供读取libpq相关权限："
  echo_green "php编译sqlite3扩展，使用指定sqlite3自带的pkgconfig时，需要提供读取对应目录的权限："
  echo_cyan "usermod -a -G postgres,sqlite php-fpm"
  echo_green "如果使用 apt install libpq-dev libsqlite3-dev -y 依赖包则不需要"
  echo_yellow " "
  echo_green "此版本PHP使用指定Postgres安装目录并在编译时禁用了sqlite扩展"
  echo_yellow "=================================================================="
  usermod -a -G postgres php-fpm
}

#开发用户追加权限
devUserPower(){
  devUserName=$1
  echo_yellow "=================================================================="
  echo_green "开发用户追加权限"
  echo_yellow "web/php文件所属用户都是开发用户，所以nginx和php-fpm用户需要追加开发组"
  echo_yellow " "
  echo_red "部署环境请注释此函数，开发环境需要开启"
  echo_red "部署环境不需要开发用户，可直接使用 nginx 用户作为 ftp、ssh 等上传工具的用户"
  echo_yellow "=================================================================="
  usermod -a -G $devUserName nginx
  usermod -a -G $devUserName php-fpm
  usermod -a -G postgres,php-fpm,nginx $devUserName
}

#安装依赖包
installPackage(){
  echo_yellow "=================================================================="
  echo_green "安装依赖"
  echo_green "确保 PostgreSQL/PHP/Nginx 必备依赖项"
  echo_green "debian12 发行版，如因依赖导致部分功能异常，自行安装相应依赖包即可"
  echo_red "注意1：该lnmpp包不兼容其他发行版，因为极有可能因为依赖问题，导致整个环境无法使用"
  echo_red "注意2：部分依赖包在部署阶段可能没用，但由于没对单个功能测试，只能选择安装全部依赖"
  echo_yellow "=================================================================="
  apt install -y make gcc g++ clang pkg-config autoconf libicu-dev liblz4-dev \
  libzstd-dev libbison-dev flex libreadline-dev zlib1g-dev libssl-dev \
  libpam0g-dev uuid-dev libsystemd-dev libcurl4-openssl-dev libpng-dev \
  libavif-dev libwebp-dev libjpeg-dev libxpm-dev libfreetype-dev libonig-dev \
  libcapstone-dev libsodium-dev libzip-dev libxslt1-dev libgd-dev libgeoip-dev
}

#安装预构建包
InstallBuild(){
  echo_yellow "=================================================================="
  echo_green "解压lnmpp预构建包\n含两个目录"
  echo_yellow " "
  echo_cyan "/server"
  echo_cyan "/www"
  echo_yellow " "
  echo_green "预先编译成功的lnmpp解压到服务器目录下"
  echo_yellow "=================================================================="
  tar -xJf ./lnmpp.tar.xz -C /
}

#重置PostgreSQL数字证书
resetPgsqlCertificate(){
  pgsqlTlsPath=/server/etc/postgres/tls
  pgsqlTlsScriptPath=/server/etc/postgres/gen-test-certs.sh
  rm -rf $pgsqlTlsPath
  echo_yellow "=================================================================="
  echo_green "创建一键生成PostgreSQL数字证书脚本"
  echo_yellow " "
  echo_cyan "注意: 不能向其他用户开放权限"
  echo_cyan "开发环境: 目录 750/ 文件 640"
  echo_cyan "部署环境: 目录 700/ 文件 600"
  echo_yellow " "
  echo_yellow "=================================================================="
  echo_cyan "[+] Create PostgreSQL certs script..."
  echo "#\!/bin/bash
generate_cert() {
    local name=\$1
    local cn=\"\$2\"
    local opts=\"\$3\"

    local keyfile=$pgsqlTlsPath/\${name}.key
    local certfile=$pgsqlTlsPath/\${name}.crt

    [ -f \$keyfile ] || openssl genrsa -out \$keyfile 2048
    openssl req \\
        -new -sha256 \\
        -subj \"/O=PostgreSQL Test/CN=\$cn\" \\
        -key \$keyfile | \\
        openssl x509 \\
            -req -sha256 \\
            -CA $pgsqlTlsPath/root.crt \\
            -CAkey $pgsqlTlsPath/root.key \\
            -CAserial $pgsqlTlsPath/root.txt \\
            -CAcreateserial \\
            -days 365 \\
            \$opts \\
            -out \$certfile
}

mkdir $pgsqlTlsPath
[ -f $pgsqlTlsPath/root.key ] || openssl genrsa -out $pgsqlTlsPath/root.key 4096
openssl req \\
    -x509 -new -nodes -sha256 \\
    -key $pgsqlTlsPath/root.key \\
    -days 3650 \\
    -subj '/O=PostgreSQL Test/CN=Certificate Authority' \\
    -out $pgsqlTlsPath/root.crt

cat > $pgsqlTlsPath/openssl.cnf <<_END_
[ server_cert ]
keyUsage = digitalSignature, keyEncipherment
nsCertType = server

[ client_cert ]
keyUsage = digitalSignature, keyEncipherment
nsCertType = client
_END_

generate_cert server \"Server-only\" \"-extfile $pgsqlTlsPath/openssl.cnf -extensions server_cert\"
generate_cert client \"Client-only\" \"-extfile $pgsqlTlsPath/openssl.cnf -extensions client_cert\"
generate_cert client-admin \"admin\" \"-extfile $pgsqlTlsPath/openssl.cnf -extensions client_cert\"
generate_cert client-emad \"emad\" \"-extfile $pgsqlTlsPath/openssl.cnf -extensions client_cert\"
generate_cert pgsql \"Generic-cert\"

[ -f $pgsqlTlsPath/pgsql.dh ] || openssl dhparam -out $pgsqlTlsPath/pgsql.dh 2048
" > $pgsqlTlsScriptPath
  echo_cyan "[+] run PostgreSQL certs script..."
  chmod +x $pgsqlTlsScriptPath
  $pgsqlTlsScriptPath
  echo_cyan "tls证书重置完成，是否删除一键生成PostgreSQL证书脚本(1删除/默认不删除)："
  read isDeletePgsqlTlsScript
  if [[ "$isDeletePgsqlTlsScript" == '1' ]]; then
    rm $pgsqlTlsScriptPath
  fi
}

#修改文件权限
modFilePower(){
  echo_yellow "=================================================================="
  echo_green "文件权限"
  echo_green "通常来讲压缩包里含的权限是正确的，这里重新执行一遍，更加稳妥"
  echo_yellow "=================================================================="
  echo_green "/server 目录权限"
  chown root:root -R /server
  chmod 755 /server
  find /server/default -type f -exec chmod 644 {} \;
  find /server/default -type d -exec chmod 755 {} \;

  echo_green "/www 目录权限"
  echo_red "开发环境使用emad用户（nginx/php-fpm 需加入 emad用户组）"
  echo_red "部署环境通常是www用户（nginx/php-fpm 需加入 www用户组）"
  chmod 750 /www

  echo_green "postgres文件权限"
  chown postgres:postgres -R /server/postgres /server/pgData /server/logs/postgres /server/etc/postgres
  find /server/postgres /server/logs/postgres /server/etc/postgres -type f -exec chmod 640 {} \;
  find /server/postgres /server/logs/postgres /server/etc/postgres -type d -exec chmod 750 {} \;
  chmod 750 -R /server/postgres/bin
  find /server/etc/postgres/tls /server/pgData -type f -exec chmod 600 {} \;
  find /server/etc/postgres/tls /server/pgData -type d -exec chmod 700 {} \;

  echo_green "php文件权限"
  chown php-fpm:php-fpm -R /server/php /server/logs/php
  find /server/php /server/logs/php -type f -exec chmod 640 {} \;
  find /server/php /server/logs/php -type d -exec chmod 750 {} \;
  chmod 640 /server/php/84/lib/php/extensions/no-debug-non-zts-*/*
  chmod 750 -R /server/php/84/{bin,sbin}
  chmod 750 /server/php/tools/{composer,php-cs-fixer-v3}.phar

  echo_green "nginx文件权限"
  chown nginx:nginx -R /server/{nginx,sites}
  chown nginx:nginx -R /server/{etc,logs}/nginx
  find /server/{nginx,sites} -type f -exec chmod 640 {} \;
  find /server/{nginx,sites} -type d -exec chmod 750 {} \;
  find /server/etc/nginx -type f -exec chmod 640 {} \;
  find /server/etc/nginx -type d -exec chmod 750 {} \;
  chmod 750 /server/logs/nginx
  chmod 750 -R /server/nginx/sbin
  echo_green "为nginx启用CAP_NET_BIND_SERVICE能力"
  echo_red "注：每次修改nginx执行文件权限，都需要重新启用该能力"
  setcap cap_net_bind_service=+eip /server/nginx/sbin/nginx
}

#安装systemctl单元
InstallSystemctlUnit(){
  echo_yellow "=================================================================="
  echo_green "加入systemctl守护进程\n含systemctl unit文件"
  echo_yellow " "
  echo_cyan "/lib/systemd/system/{postgres,php84-fpm,nginx}.service"
  echo_yellow " "
  echo_green "支持开启自动启动服务，非常规终止进程会自动启动服务"
  echo_yellow "=================================================================="
  echo_cyan "[+] Create postgres service..."
  echo "[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
User=postgres
Group=postgres
RuntimeDirectory=postgres
RuntimeDirectoryMode=0750
ExecStart=/server/postgres/bin/postgres -D /server/pgData
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=infinity

[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/postgres.service

  echo_cyan "[+] Create php84-fpm service..."
  echo "[Unit]
Description=The PHP 8.4 FastCGI Process Manager
After=network.target

[Service]
Type=notify
User=php-fpm
Group=php-fpm
RuntimeDirectory=php84-fpm
RuntimeDirectoryMode=0750
ExecStart=/server/php/84/sbin/php-fpm --nodaemonize --fpm-config /server/php/84/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 \$MAINPID
PrivateTmp=true
ProtectSystem=full
PrivateDevices=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictRealtime=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK AF_UNIX
RestrictNamespaces=true

[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/php84-fpm.service

  echo_cyan "[+] Create nginx service..."
  echo "[Unit]
Description=nginx-1.28.x
After=network.target

[Service]
Type=forking
User=nginx
Group=nginx
RuntimeDirectory=nginx
RuntimeDirectoryMode=0750
ExecStartPre=/server/nginx/sbin/nginx -t
ExecStart=/server/nginx/sbin/nginx -c /server/nginx/conf/nginx.conf
ExecReload=/server/nginx/sbin/nginx -s reload
ExecStop=/server/nginx/sbin/nginx -s quit
Restart=on-failure
PrivateTmp=true

[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/nginx.service

  echo_green "Registered Service..."
  systemctl daemon-reload
  systemctl enable --now {postgres,php84-fpm,nginx}.service
}

#日志管理
LogManagement(){
  echo_yellow "=================================================================="
  echo_green "Nginx 使用 Logrotate 来管理日志文件"
  echo_yellow "=================================================================="
  echo_cyan "[+] 创建 nginx 的 Logrotate 脚本..."
  echo "
/server/logs/nginx/access/*.log {
    daily
    maxsize 100M
    missingok
    rotate 30
    compress
    delaycompress
    dateext
    dateformat -%Y%m%d.%s
    notifempty
    create 0640 nginx nginx
    sharedscripts
    postrotate
        if [ -f /run/nginx/process.pid ]; then
            /usr/bin/kill -USR1 \$(/bin/cat /run/nginx/process.pid)
        fi
    endscript
}

/server/logs/nginx/error/*.log {
    monthly
    maxsize 100M
    missingok
    rotate 12
    compress
    delaycompress
    dateext
    dateformat -%Y%m%d.%s
    notifempty
    create 0640 nginx nginx
    sharedscripts
    postrotate
        if [ -f /run/nginx/process.pid ]; then
            /usr/bin/kill -USR1 \$(/bin/cat /run/nginx/process.pid)
        fi
    endscript
}
" > /etc/logrotate.d/nginx
}

#日志管理
LogManagement(){
  echo_yellow "=================================================================="
  echo_green "系统级优化"
  echo_yellow "=================================================================="
  echo_cyan "[+] 内核管理..."
  echo "vm.overcommit_memory = 1" > /etc/sysctl.d/overcommit_memory.conf
  echo "net.core.somaxconn = 4096" > /etc/sysctl.d/somaxconn.conf
  echo "net.ipv4.tcp_max_syn_backlog = 4096" > /etc/sysctl.d/tcp_max_syn_backlog.conf

  echo_cyan "[+] postgres用户资源管理..."
  echo "postgres  soft  nofile  65535
postgres  hard  nofile  65535" > /etc/security/limits.d/postgres.conf
}

#PHP工具软链接到 /usr/local/bin
LogManagement(){
  echo_yellow "=================================================================="
  echo_green "PHP工具软链接到 /usr/local/bin"
  echo_green "含：composer和php-cs-fixer"
  echo_yellow "=================================================================="
  ln -s /server/php/tools/composer.phar /usr/local/bin/composer
  ln -s /server/php/tools/php-cs-fixer-v3.phar /usr/local/bin/php-cs-fixer
}

#安装nginx站点管理工具
InstallSiteManagement(){
  echo_yellow "=================================================================="
  echo_green "安装nginx站点管理工具"
  echo_yellow "=================================================================="
  echo_cyan "[+] 创建nginx站点管理工具..."
  echo "#\!/usr/bin/env bash

# ==============================================
# nginxctl - Nginx 站点管理工具
# 功能: 启用/禁用 Nginx 站点配置
# 作者: 地上马
# ==============================================

# 定义路径
AVAILABLE_DIR=\"/server/sites/available\"
ENABLED_DIR=\"/server/sites/enabled\"

# 颜色定义
RED='\\\033[0;31m'
GREEN='\\\033[0;32m'
YELLOW='\\\033[1;33m'
BLUE='\\\033[0;34m'
NC='\\\033[0m' # No Color

# 打印带格式的消息函数
print_success() {
    echo -e \"\${GREEN}[SUCCESS] \$1\${NC}\"
}

print_error() {
    echo -e \"\${RED}[ERROR] \$1\${NC}\"
}

print_info() {
    echo -e \"\${BLUE}[INFO] \$1\${NC}\"
}

print_warning() {
    echo -e \"\${YELLOW}[WARNING] \$1\${NC}\"
}

# 检查必要目录是否存在
check_directories() {
    if [ ! -d \"\$AVAILABLE_DIR\" ]; then
        print_error \"目录不存在: \$AVAILABLE_DIR\"
        exit 1
    fi

    if [ ! -d \"\$ENABLED_DIR\" ]; then
        print_error \"目录不存在: \$ENABLED_DIR\"
        exit 1
    fi
}

# 执行启用操作
enable_site() {
    local SITE_NAME=\$1
    local CONF_FILE=\"\${SITE_NAME}.nginx\"
    local AVAILABLE_FILE=\"\${AVAILABLE_DIR}/\${CONF_FILE}\"
    local ENABLED_FILE=\"\${ENABLED_DIR}/\${CONF_FILE}\"

    # 检查可用配置文件是否存在
    if [ ! -f \"\$AVAILABLE_FILE\" ]; then
        print_error \"配置文件 \${AVAILABLE_FILE} 不存在\"
        exit 1
    fi

    # 检查是否已启用
    if [ -L \"\$ENABLED_FILE\" ]; then
        print_warning \"站点 \${SITE_NAME} 已启用，无需重复操作\"
        exit 0
    fi

    # 创建软链接
    if ! ln -s \"\$AVAILABLE_FILE\" \"\$ENABLED_FILE\"; then
        print_error \"创建软链接失败\"
        exit 1
    fi

    print_success \"已启用站点 \${SITE_NAME}\"

    # 测试Nginx配置
    print_info \"测试Nginx配置...\"
    if ! nginx -t; then
        print_error \"Nginx 配置测试失败，请检查配置\"
        exit 1
    fi

    # 重载nginx
    print_info \"重载Nginx配置...\"
    if ! systemctl reload nginx; then
        print_error \"重载Nginx失败\"
        exit 1
    fi

    print_success \"Nginx已成功重载\"
}

# 执行禁用操作
disable_site() {
    local SITE_NAME=\$1
    local CONF_FILE=\"\${SITE_NAME}.nginx\"
    local ENABLED_FILE=\"\${ENABLED_DIR}/\${CONF_FILE}\"

    # 检查是否已启用
    if [ ! -L \"\$ENABLED_FILE\" ]; then
        print_warning \"站点 \${SITE_NAME} 未启用，无需操作\"
        exit 0
    fi

    # 删除软链接
    if ! rm \"\$ENABLED_FILE\"; then
        print_error \"删除软链接失败\"
        exit 1
    fi

    print_success \"已禁用站点 \${SITE_NAME}\"

    # 测试Nginx配置
    print_info \"测试Nginx配置...\"
    if ! nginx -t; then
        print_error \"Nginx 配置测试失败，请检查配置\"
        exit 1
    fi

    # 重载nginx
    print_info \"重载Nginx配置...\"
    if ! systemctl reload nginx; then
        print_error \"重载Nginx失败\"
        exit 1
    fi

    print_success \"Nginx已成功重载\"
}

# 显示帮助信息
show_help() {
    echo -e \"\${BLUE}nginxctl - Nginx站点管理工具\${NC}\"
    echo \"\"
    echo -e \"\${BLUE}用法:\${NC}\"
    echo -e \"  nginxctl enable <site_name>   \${GREEN}启用指定站点\${NC}\"
    echo -e \"  nginxctl disable <site_name>  \${RED}禁用指定站点\${NC}\"
    echo -e \"  nginxctl help                 \${BLUE}显示帮助信息\${NC}\"
    echo \"\"
    echo -e \"\${BLUE}示例:\${NC}\"
    echo -e \"  \${GREEN}nginxctl enable demo\${NC}\"
    echo -e \"  \${RED}nginxctl disable demo\${NC}\"
    echo \"\"
    echo -e \"\${BLUE}说明:\${NC}\"
    echo -e \"  站点配置文件应位于 \${AVAILABLE_DIR}/ 目录中\"
    echo -e \"  启用后会在 \${ENABLED_DIR}/ 创建软链接\"
}

# 主程序逻辑
main() {
    # 检查必要目录是否存在
    check_directories

    # 检查参数数量
    if [ \$# -lt 1 ]; then
        show_help
        exit 1
    fi

    ACTION=\$1
    SITE_NAME=\$2

    # 根据输入参数执行相应操作
    case \"\$ACTION\" in
        enable)
            if [ \$# -ne 2 ]; then
                print_error \"启用站点需要指定站点名称\"
                echo \"用法: nginxctl enable <site_name>\"
                exit 1
            fi
            enable_site \"\$SITE_NAME\"
            ;;
        disable)
            if [ \$# -ne 2 ]; then
                print_error \"禁用站点需要指定站点名称\"
                echo \"用法: nginxctl disable <site_name>\"
                exit 1
            fi
            disable_site \"\$SITE_NAME\"
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            print_error \"无效操作 '\${ACTION}'\"
            echo \"可用操作: enable, disable\"
            echo \"使用 'nginxctl help' 查看完整帮助\"
            exit 1
            ;;
    esac
}

# 执行主程序
main \"\$@\"" > /usr/local/bin/nginxctl

  echo_cyan "站点管理工具授权..."
  chmod 755 /usr/local/bin/nginxctl
}

echo_cyan "解压脚本同级目录下需存在源码压缩包 lnmpp.tar.xz"
echo_cyan "是否退出(1退出/默认继续)："
read isExit
if [[ "$isExit" == '1' ]]; then
  exit 0
fi

#系统更新到最新
upgradeOS

echo_cyan "是否重启操作系统(1重启/默认不重启)："
read num

if [[ "$num" == '1' ]]; then
  echo_cyan "停止向下执行，并重启系统"
  sync;sync;sync;reboot
else
  #清理旧数据
  cleanOldData
  echo ' '
  #创建用户
  createUser
  echo ' '
  #开发用户追加权限，部署环境请注释掉
  echo_red "部署环境通常不需要授权"
  echo_cyan "输入开发用户名，为其授权(为空不授权)："
  read userName
  if [[ -n "$userName" ]]; then
    devUserPower $userName
  fi
  echo ' '
  #安装依赖包
  installPackage
  echo ' '
  #解压lnmpp预构建包到指定目录
  InstallBuild
  echo ' '
  #是否重新生成tls证书
  echo_cyan "是否重置数字证书(1重置/默认不重置)："
  read isResetCertificate
  if [[ "$isResetCertificate" == '1' ]]; then
    resetPgsqlCertificate
  fi
  echo ' '
  #修改文件权限
  modFilePower
  echo ' '
  #安装systemctl单元
  InstallSystemctlUnit
  #日志管理
  LogManagement
  #系统级调优
  SystemdOptimize
  #安装nginx站点管理工具
  InstallSiteManagement
  echo ' '
  echo_yellow "=================================================================="
  echo_green "lnmpp安装完成"
  echo_yellow "   - Postgres 默认有个超级管理员用户 admin 密码 1"
  echo_yellow "=================================================================="
  echo ' '
  echo_yellow "=================================================================="
  echo_green "systemctl 常用指令"
  echo_yellow "重载 systemctl"
  echo_yellow "systemctl daemon-reload"
  echo_yellow "启用并开启服务"
  echo_yellow "systemctl enable --now {postgres,php84-fpm,nginx}.service"
  echo_yellow "禁用并禁止服务"
  echo_yellow "systemctl disable --now {postgres,php84-fpm,nginx}.service"
  echo_yellow "开启"
  echo_yellow "systemctl start {postgres,php84-fpm,nginx}.service"
  echo_yellow "停止"
  echo_yellow "systemctl stop {postgres,php84-fpm,nginx}.service"
  echo_yellow "查看状态"
  echo_yellow "systemctl status {postgres,php84-fpm,nginx}.service"
  echo_yellow "重新加载配置(部分服务器不支持重载配置文件)"
  echo_yellow "systemctl reload nginx"
  echo_yellow "使用nginx站点管理工具"
  echo_yellow "启用站点： nginxctl enable <site_name>"
  echo_yellow "禁用站点： nginxctl disable <site_name>"
  echo_yellow "获取帮助： nginxctl help"
  echo_yellow "=================================================================="
  echo ' '
  echo_yellow "=================================================================="
  echo_red "条件允许，建议重启系统！！！"
  echo_yellow "=================================================================="
fi
